package sy.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import sy.pageModel.BaseEntity;
import sy.pageModel.DataGrid;
import sy.pageModel.Json;
import sy.pageModel.Partner;
import sy.service.PartnerServiceI;
import sy.util.StringUtil;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016-06-29.
 */
@Controller()
@RequestMapping("partnerController")
public class PartnerController extends BaseController {

    @Autowired
    private PartnerServiceI partnerService;
    @Autowired
    private HttpSession session;
    @RequestMapping("/datagrid.action")
    @ResponseBody
    public DataGrid findPartnerAction(Partner partner){
        return partnerService.detagrid(partner);
    }

    @RequestMapping("/find_dept_type.action")
    @ResponseBody
    public Json findDeptType(){
        Json js=new Json();
        js.setObj(findOption(partnerService.findTypeList()));
        return js;
    }

    @RequestMapping("/find_dept.action")
    @ResponseBody
    public Json findDeptList(){
        Json js=new Json();
        String res="";
        for (BaseEntity entity:partnerService.findDeptList()) {
            res+="<option value='"+entity.getId()+"'>"+"["+entity.getId()+"]"+entity.getName()+"</option>";
        }
        js.setObj(res);
        return js;
    }


    /**
     * 添加合作伙伴信息
     * @param partner
     * @return
     */
    @RequestMapping("/add_partner.action")
    @ResponseBody
    public Json addpartnerAction(Partner partner){
        int rows=0;
        Map map=StringUtil.findMap(partner.getPic());
        partner.setPic((String) map.get("pic"));
        partner.setId((String) map.get("id"));
        try {
            rows=partnerService.addPartnerService(partner);
            if (rows>0) logUserAction(session,"partnerController/add_partner.action",partner.toString());
        } catch (Exception e) {
            e.printStackTrace();
            rows=0;
        }
        Json js= StringUtil.findJson(rows,"添加");
        return js;
    }

    /**
     * 修改合作伙伴信息
     * @param partner
     * @return
     */
    @RequestMapping("/edit_partner.action")
    @ResponseBody
    public Json editpartnerAction(Partner partner){
        int rows=0;
        partner.setPic(StringUtil.findStr(partner.getPic()));
        try {
            rows=partnerService.editPartnerService(partner);
            if (rows>0) logUserAction(session,"partnerController/edit_partner.action",partner.toString());
        } catch (Exception e) {
            e.printStackTrace();
            rows=0;
        }
        Json js= StringUtil.findJson(rows,"修改");
        return js;
    }

    /**
     * 删除合作伙伴信息
     * @param partner
     * @return
     */
    @RequestMapping("/del_partner.action")
    @ResponseBody
    public Json delpartnerAction(Partner partner){
        int rows=0;
        try {
            rows= partnerService.deletePartnerService(partner);
            if (rows>0)   logUserAction(session,"partnerController/edit_partner.action",partner.toString());
        } catch (Exception e) {
            e.printStackTrace();
            rows=0;
        }
        Json js= StringUtil.findJson(rows,"删除");
        return js;
    }
}
